package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.GoodsAttrValues;
import com.ktmall.ktmalluijava.service.GoodsAttrValuesService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/19 上午8:40
 */
@RestController
@RequestMapping("/goods/attr/values")
public class GoodsAttrValuesController {

    @Resource
    private GoodsAttrValuesService service;

    @GetMapping("/list")
    public String select(Integer goodsId, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        Page<GoodsAttrValues> page = service.getPage(goodsId,pageNo,pageSize);
        return ResMsg.success("商品关联属性列表加载成功",page);
    }

    @PostMapping("/add")
    public String add(@RequestBody GoodsAttrValues goodsAttrValues){
        int i = service.add(goodsAttrValues);
        if(i>0){
            return ResMsg.success("新增成功");
        }else{
            return ResMsg.error("新增失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id){
        GoodsAttrValues goodsAttrValues ;
        try{
            goodsAttrValues = service.detail(id);
        }catch(Exception e){
            return ResMsg.error("商品关联属性详情加载失败",e);
        }
        return ResMsg.success("商品关联属性详情加载成功",goodsAttrValues);
    }

    @PostMapping("/update")
    public String update(@RequestBody GoodsAttrValues goodsAttrValues){
        int i = service.update(goodsAttrValues);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }

    @PostMapping("/del")
    public String delete(@RequestBody Integer... id){
        int i = service.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return ResMsg.error("删除失败");
        }
    }

    @GetMapping("/all")
    public String getAll(Integer goodsId){
        return ResMsg.success("全部商品管理属性加载成功",service.getAll(goodsId));
    }
}
